{% init op = "view" %}
{% init error = "" %}

<script type="text/javascript" src="/static/lib/wangEditor/wangEditor-3.0.10.min.js"></script>
{% if op == "view" %}
    {% include "file/view_header.html" %}

    <hr/>
    <div id="editor">
        {% raw file.data %}
    </div>
    <br/>
{% else %}
    {% set rows = file.data.count('\n') %}
    <div class="alert">{{error}}</div>
    <form method="POST" enctype="multipart/form-data">
        <input name="version" value="{{file.version}}" class="hide">
        <input name="type" value="html" class="hide"/>
        <textarea id="data" name="data" style="width:100%;margin-top:10px;font-family:monospace;" rows={{rows}} class="hide">{{file.data}}</textarea>

        <div id="toolbar" style="border:1px solid #ccc;background-color: #f1f1f1;"></div>
        <div id="editor" style="height: 400px;border:1px solid #ccc;border-top: none;">
        </div>
    </form>

    <div style="margin-top: 5px;">
        <input id="saveBtn" type="button" value="保存">
        <input type="button" class="link-btn" href="/file/view?id={{file.id}}" value="预览">
    </div>


    <script type="text/javascript">
    var editor = new wangEditor('#toolbar', "#editor");
    editor.customConfig.uploadImgShowBase64 = true   // 使用 base64 保存图片
    editor.create();
    editor.txt.html($("#data").text())

    $("#saveBtn").click(function () {
        var data = editor.txt.html();
        $.post("/file/autosave?type=html", {id:{{file.id}}, data:data}, function (resp) {
            console.log(resp);
            if (resp.code == "success") {
                window.location.reload();
            } else {
                alert(resp.message);
            }
        })
    })
    </script>


{% end %}
